package lev01_beginnerClass.class02_sort;

import java.util.Arrays;

/**
 * buble sort
 */
public class L02_bubleSort {

    public static void main(String[] args) {
        int[] arr = {4,5,6,7,5,3,45,6,7,8,3,2,55,6,78,};
        //before
        System.out.println(Arrays.toString(arr));
        //after
        bubleSort(arr);
        System.out.println(Arrays.toString(arr));
    }


    public static void bubleSort(int[] arr){
        //give priority to boundary conditon
        if(arr == null || arr.length <=1){
            return;
        }

        int len = arr.length;

        for(int i = len-1;i>=0;i--){
            for(int second = 1;second <= len-1;second++){
                if(arr[second] < arr[second-1]){
                    swap(arr,second,second-1);
                }
            }
        }

    }

    public static void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
